package com.example.controller;

import com.example.redis.CaptchaRedis;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import org.springframework.ui.Model;

@Controller
public class LoginController {

	@Autowired
	private CaptchaRedis captchaRedis;

	@GetMapping("/login")
	public String login(@RequestParam(value = "redirect", required = false) String redirect, HttpSession session, Model model) {
		// 生成 UUID
		String uuid = UUID.randomUUID().toString();
		// 将 UUID 添加到模型中
		model.addAttribute("uuid", uuid);
		// 将 redirect 参数添加到模型中
		model.addAttribute("redirect", redirect);

		return "login";
	}

	@GetMapping("/messages")
//	@ResponseBody
	public String getMessages() {

		return "meassage";
	}
	@GetMapping("/meassage-callback")
//	@ResponseBody
	public String getMessagescallback() {
		return "meassage-callback";
	}

	@GetMapping("/home")
	public String home() {
		return "home";
	}


	@GetMapping("/getNewUuid")
	@ResponseBody
	public String getNewUuid() {
		return UUID.randomUUID().toString();
	}


	@GetMapping("/captchab/{uuid}")
	@ResponseBody
	public void getCaptcha(HttpServletResponse response, @PathVariable("uuid") String uuid) throws IOException {
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/gif");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		//生成验证码
		SpecCaptcha captcha = new SpecCaptcha(150, 40);
		captcha.setLen(5);
		captcha.setCharType(Captcha.TYPE_DEFAULT);

		//保存验证码
		captchaRedis.set(uuid, captcha.text());

		captcha.out(response.getOutputStream());
	}







}
